/**
 * @Title component
 * @Date 2023-02-31 10:51:14
 * @Remark
 */

// react & lib & axios
import find from "lodash/find";
// store
import { userState } from "state/global";

// 控制菜单权限
export const permMenuVerify = (menuUrl: string) => {
  const list = userState.getAccount().menuList;
  const flag = find(list, { menuUrl });
  if (!flag) {
    return false;
  }
  return true;
};

// 控制按钮dom结构-接口权限
export const permVerify = (permission: string) => {
  const list = userState.getAccount().permissions;
  const flag = find(list, { permission });
  if (!flag) {
    return false;
  }
  return true;
};

// 控制列表dom结构-接口权限
export const permissionVerify = (
  component: JSX.Element,
  permission: string,
  holder?: JSX.Element,
) => {
  const list = userState.getAccount().permissions;
  const flag = find(list, { permission });

  if (!flag) {
    if (holder) {
      return holder;
    }
    return null;
  }
  return component;
};

export const PERMISSION_DICT = {
  // 业务管理
  CASES_LIST: "/renshou/cases/list", // 案件办理-列表
  CASES_UPDATE: "/renshou/cases/update", // 案件办理-修改
  CASES_DELETE: "/renshou/cases/delete", // 案件办理-作废
  CASES_DEALWITH: "/renshou/cases/dealWith", // 案件办理-处置
  CASES_TRANSFERRED: "/renshou/cases/transferred", // 案件办理-转办
  CASES_GOBACK: "/renshou/cases/goBack", // 案件办理-退回
  CASES_NO_FILLING: "/renshou/cases/no_filing", // 案件办理-不立案
  CASES_FLED: "/renshou/cases/fled", // 案件办理-立案
  CASES_DETAIL: "/renshou/cases/detail", // 案件办理-详情
  CASES_URGE: "/renshou/cases/urge", // 案件办理-催办
  CASES_UPLOADEVIDENCE: "/renshou/cases/uploadEvidence", // 案件办理-上报证据
  CASES_RESURVEY: "/renshou/cases/resurvey", // 案件办理-重新调查
  CASES_REFUSE_TO_EXECUTE: "/renshou/cases/refuse_to_execute", // 案件办理-拒不执行
  CASES_CLOSED: "/renshou/cases/closed", // 案件办理-结案
  CASES_REDISPOSE: "/renshou/cases/redispose", // 案件办理-重新处置
  CASES_CANCELLATION: "/renshou/cases/cancellation", // 案件办理-取消立案
  CASES_SUPERVISE: "/renshou/cases/supervise", // 案件办理-督办
  CASES_REGISTRATION: "/renshou/cases/registration", // 案件办理-新增
  CASES_HANDLEAPPROVALAGREE: "/renshou/cases/handleApprovalAgree", // 处置审批-同意
  CASES_DEPARTMENTALADD: "/renshou/cases/departmental_coordination_add", // 部门协同-新增
  CASES_HANDLINGADD: "/renshou/cases/handling_complaints_add", // 投诉办理-新增
  CASES_INSPECTIONADD: "/renshou/cases/inspection_handling_add", // 巡查办理-新增
  RANDOMTASK_LIST: "/renshou/supervision_enforcers/list", // 勤务管理-双随机任务列表
  RANDOMTASK_SETUP: "/renshou/supervision_enforcers/setUp", // 勤务管理-下发双随机任务
  INSPECTIONRECORDS_LIST: "/renshou/inspectionRecords/list", // 勤务管理-巡查/检查任务列表
  INSPECTIONRECORDS_ADD: "/renshou/inspectionRecords/add", // 勤务管理-添加巡查/检查任务
  CASETASK_LIST: "/renshou/caseTask/list", // 勤务管理-案件任务列表
  CASETASK_ADD: "/renshou/caseTask/add", // 勤务管理-添加案件任务
  ITEM_LIST: "/renshou/goods/list", // 物品管理-列表
  ITEM_EXPORT: "/renshou/goods/export", // 物品管理-导出
  CASES_CANCELSUPERVISE: "cases/cancelSupervise", // 综合查询-取消督办
  SUPERVISE_LIST: "/renshou/cases/supervise_list", // 综合查询-督办查询列表
  URGE_LIST: "/renshou/cases/urge_list", // 综合查询-催办查询列表
  REPORTED_LIST: "/renshou/cases/reported_list", // 综合查询-已报案件列表
  CLOSED_LIST: "/renshou/cases/closed_list", // 综合查询-已结案件列表
  // 统计分析
  CASE_COUNT_LIST: "/renshou/statistical_analysis/case_statistical", // 案件统计-列表
  DEPT_COUNT_LIST: "/renshou/statistical_analysis/dept_statistical", // 部门统计-列表
  STATUS_COUNT_LIST: "/renshou/statistical_analysis/case_status_statistical", // 状态统计-列表
  ILLEGAL_COUNT_LIST:
    "/renshou/statistical_analysis/violation_type_statistical", // 违法类型统计-列表
  HIGHCASE_COUNT_LIST: "/renshou/statistical_analysis/high_incidence_cases", // 高发案件统计-列表
  INSPECTION_COUNT_LIST: "/renshou/statistical_analysis/inspection_statistical", // 巡查案件统计-列表
  CASEMOM_COUNT_LIST: "/renshou/statistical_analysis/ring_analysis", //案件环比分析-列表
  // 资源管理
  REGULATORYTYPE_LIST: "/renshou/RegulatoryType/list", // 监管类型-列表
  REGULATORYTYPE_ADD: "/renshou/RegulatoryType/add", // 监管类型-新增
  REGULATORYTYPE_UPDATE: "/renshou/RegulatoryType/update", // 监管类型-修改
  REGULATORYTYPE_DEL: "/renshou/RegulatoryType/delete", // 监管类型-删除
  PROFESSIONALTYPE_LIST: "/renshou/ProfessionalType/list", // 专业类型-列表
  PROFESSIONALTYPE_ADD: "/renshou/ProfessionalType/add", // 专业类型-新增
  PROFESSIONALTYPE_UPDATE: "/renshou/ProfessionalType/update", // 专业类型-修改
  PROFESSIONALTYPE_DEL: "/renshou/ProfessionalType/delete", // 专业类型-删除
  INSPECTIONTYPE_LIST: "/renshou/inspectionType/list", // 检查类型管理-列表
  INSPECTIONTYPE_ADD: "/renshou/inspectionType/add", // 检查类型管理-新增
  INSPECTIONTYPE_UPDATE: "/renshou/inspectionType/update", // 检查类型管理-修改
  INSPECTIONTYPE_DEL: "/renshou/inspectionType/delete", // 检查类型管理-删除
  INSPECTIONTYPE_EXPORT: "/renshou/inspectionType/export", // 检查类型管理-导出
  USEFULEXPRESSIONS_LIST: "/renshou/UsefulExpressions/list", // 常用短语管理-列表
  USEFULEXPRESSIONS_ADD: "/renshou/UsefulExpressions/add", // 常用短语管理-新增
  USEFULEXPRESSIONS_UPDATE: "/renshou/UsefulExpressions/update", // 常用短语管理-修改
  USEFULEXPRESSIONS_DEL: "/renshou/UsefulExpressions/delete", // 常用短语管理-删除
  INSPECTIONITEMS_LIST: "/renshou/inspectionItems/list", // 检查事项管理-列表
  INSPECTIONITEMS_ADD: "/renshou/inspectionItems/add", // 检查事项管理-新增
  INSPECTIONITEMS_UPDATE: "/renshou/inspectionItems/update", // 检查事项管理-修改
  INSPECTIONITEMS_DEL: "/renshou/inspectionItems/delete", // 检查事项管理-删除
  INSPECTIONITEMS_EXPORT: "/renshou/inspectionItems/export", // 检查事项管理-导出
  ENFORCERS_LIST: "/renshou/enforcers/list", // 执法人员管理-列表
  ENFORCERS_ADD: "/renshou/enforcers/add", // 执法人员管理-新增
  ENFORCERS_DETAIL: "/renshou/enforcers/detail", // 执法人员管理-详情
  ENFORCERS_DEL: "/renshou/enforcers/delete", // 执法人员管理-删除
  ENFORCERS_EXPORT: "/renshou/enforcers/export", // 执法人员管理-导出
  RESOURCECAR_LIST: "/renshou/law_enforcement_vehicles/list", // 执法车辆管理-列表
  RESOURCECAR_ADD: "/renshou/law_enforcement_vehicles/add", // 执法车辆管理-新增
  RESOURCECAR_EXPORT: "/renshou/law_enforcement_vehicles/export", // 执法车辆管理-导出
  RESOURCECAR_DETAIL: "/renshou/law_enforcement_vehicles/detail", // 执法车辆管理-详情
  RESOURCECAR_DEL: "/renshou/law_enforcement_vehicles/delete", // 执法车辆管理-删除
  RESOURCECAR_UPDATE: "/renshou/law_enforcement_vehicles/update", // 执法车辆管理-修改
  SUPERVISION_LIST: "/renshou/supervision_legal_persons/list", // 监管对象（法人）-列表
  SUPERVISION_ADD: "/renshou/supervision_legal_persons/add", // 监管对象（法人）-新增
  SUPERVISION_UPDATE: "/renshou/supervision_legal_persons/update", // 监管对象（法人）-修改
  SUPERVISION_DEL: "/renshou/supervision_legal_persons/delete", // 监管对象（法人）-删除
  SUPERVISION_EXPORT: "/renshou/supervision_legal_persons/export", // 监管对象（法人）-导出
  SUPERVISION_ENABLE: "/renshou/supervision_legal_persons/enable", // 监管对象（法人）-状态
  SUPERVISION_IVIDUAL_LIST: "/renshou/supervision_individuals/list", // 监管对象（个人）-列表
  SUPERVISION_IVIDUAL_ADD: "/renshou/supervision_individuals/add", // 监管对象（个人）-新增
  SUPERVISION_IVIDUAL_UPDATE: "/renshou/supervision_individuals/update", // 监管对象（个人）-修改
  SUPERVISION_IVIDUAL_DEL: "/renshou/supervision_individuals/delete", // 监管对象（个人）-删除
  SUPERVISION_IVIDUAL_EXPORT: "/renshou/supervision_individuals/export", // 监管对象（个人）-导出
  SUPERVISION_IVIDUAL_ENABLE: "/renshou/supervision_individuals/enable", // 监管对象（个人）-状态
  ENFORCEMENT_TERMINAL_LIST: "/renshou/law_enforcement_terminals/list", // 执法终端-列表
  ENFORCEMENT_TERMINAL_ADD: "/renshou/law_enforcement_terminals/add", // 执法终端-新增
  ENFORCEMENT_TERMINAL_UPDATE: "/renshou/law_enforcement_terminals/update", // 执法终端-修改
  ENFORCEMENT_TERMINAL_DEL: "/renshou/law_enforcement_terminals/delete", // 执法终端-删除
  ENFORCEMENT_TERMINAL_EXPORT: "/renshou/law_enforcement_terminals/export", // 执法终端-导出
  ENFORCEMENT_CERTIFICATE_LIST: "/renshou/law_enforcement_licenses/list", // 执法证照-列表
  ENFORCEMENT_CERTIFICATE_ADD: "/renshou/law_enforcement_licenses/add", // 执法证照-新增
  ENFORCEMENT_CERTIFICATE_UPDATE: "/renshou/law_enforcement_licenses/update", // 执法证照-修改
  ENFORCEMENT_CERTIFICATE_DEL: "/renshou/law_enforcement_licenses/delete", // 执法证照-删除
  ENFORCEMENT_CERTIFICATE_EXPORT: "/renshou/law_enforcement_licenses/export", // 执法证照-导出
  ENFORCEMENT_AGENCY_LIST: "/renshou/law_enforcement_agencies/list", // 执法机构-列表
  ENFORCEMENT_AGENCY_ADD: "/renshou/law_enforcement_agencies/add", // 执法机构-新增
  ENFORCEMENT_AGENCY_UPDATE: "/renshou/law_enforcement_agencies/update", // 执法机构-修改
  ENFORCEMENT_AGENCY_DEL: "/renshou/law_enforcement_agencies/delete", // 执法机构-删除
  ENFORCEMENT_AGENCY_EXPORT: "/renshou/law_enforcement_agencies/export", // 执法机构-导出
  POWER_LIST: "/renshou/powerMatters/list", // 权力事项-列表
  POWER_ADD: "/renshou/powerMatters/add", // 权力事项-新增
  POWER_UPDATE: "/renshou/powerMatters/update", // 权力事项-修改
  POWER_DEL: "/renshou/powerMatters/delete", // 权力事项-删除
  POWER_EXPORT: "/renshou/powerMatters/export", // 权力事项-导出
  DISCRETION_LIST: "/renshou/discretion/list", // 自由裁量管理-列表
  DISCRETION_ADD: "/renshou/discretion/add", // 自由裁量管理-新增
  DISCRETION_UPDATE: "/renshou/discretion/update", // 自由裁量管理-修改
  DISCRETION_DEL: "/renshou/discretion/delete", // 自由裁量管理-删除
  DISCRETION_EXPORT: "/renshou/discretion/export", // 自由裁量管理-导出
  REGULATORRESEAU_LIST: "/renshou/RegulatoryGrid/list", // 监管网格管理-列表
  REGULATORRESEAU_ADD: "/renshou/RegulatoryGrid/add", // 监管网格管理-新增
  REGULATORRESEAU_UPDATE: "/renshou/RegulatoryGrid/update", // 监管网格管理-修改
  REGULATORRESEAU_DEL: "/renshou/RegulatoryGrid/delete", // 监管网格管理-删除
  // 运维管理
  USER_LIST: "/renshou/user/list", // 用户管理-列表
  USER_ADD: "/renshou/user/add", // 用户管理-新增
  USER_UPDATE: "/renshou/user/update_info_by_id", // 用户管理-修改
  USER_DEL: "/renshou/user/delete", // 用户管理-删除
  USER_RESET_PWD: "/renshou/user/reset_pass", // 用户管理-重置密码
  USER_USER_EDIT_ROLE: "/renshou/user/user_role_bind", // 用户管理-用户角色
  USER_USER_CHANGE_STATE: "/renshou/user/reset_pass", // 用户管理-用户状态
  ROLE_LIST: "/renshou/role/list", // 角色管理-列表
  ROLE_ADD: "/renshou/role/add", // 角色管理-新增
  ROLE_UPDATE: "/renshou/role/update", // 角色管理-修改
  ROLE_DEL: "/renshou/role/delete", // 角色管理-删除
  ROLE_PERMISSION_ADD: "/renshou/role/add_role_perm", // 角色管理-新增角色权限
  ROLE_PERMISSION_DEL: "/renshou/role/delete_role_perm", // 角色管理-删除角色权限
  ROLE_MENU_ADD: "/renshou/role/add_role_menu", // 角色管理-新增角色菜单
  ROLE_MENU_DEL: "/renshou/role/delete_role_menu", // 角色管理-删除角色菜单
  MENU_LIST: "/renshou/menu/list", // 菜单管理-列表
  MENU_ADD: "/renshou/menu/add", // 菜单管理-新增
  MENU_UPDATE: "/renshou/menu/update", // 菜单管理-修改
  MENU_DEL: "/renshou/menu/delete", // 菜单管理-删除
  JOBS_LIST: "/renshou/position/list", // 岗位管理-列表
  JOBS_ADD: "/renshou/position/add", // 岗位管理-新增
  JOBS_UPDATE: "/renshou/position/update", // 岗位管理-修改
  JOBS_DEL: "/renshou/position/delete", // 岗位管理-删除
  SYSTEMDICT_LIST: "/renshou/dictionaries/list", // 字典管理-列表
  SYSTEMDICT_ADD: "/renshou/dictionaries/add", // 字典管理-新增
  SYSTEMDICT_UPDATE: "/renshou/dictionaries/update", // 字典管理-修改
  SYSTEMDICT_DEL: "/renshou/dictionaries/delete", // 字典管理-删除
  PERMISSION_LIST: "/renshou/perm/list", // 权限管理-列表
  PERMISSION_ADD: "/renshou/perm/add", // 权限管理-新增
  PERMISSION_UPDATE: "/renshou/perm/update", // 权限管理-修改
  PERMISSION_DEL: "/renshou/perm/delete", // 权限管理-删除
  OPERATIONLOG_LIST: "/renshou/admin_operation_journal/list", // 日志管理-列表
  OPERATIONLOG_DEL: "/renshou/admin_operation_journal/delete", // 日志管理-删除
  RULES_LIST: "/renshou/regulations/list", // 规章制度-列表
  RULES_ADD: "/renshou/regulations/add", // 规章制度-新增
  RULES_UPDATE: "/renshou/regulations/update", // 规章制度-修改
  RULES_DEL: "/renshou/regulations/delete", // 规章制度-删除
  // 菜单权限
  CASESTATISTICAL: "/main/statistical/case", // 案件统计路由
  CASEQUERY: "/main/business/query", // 案件查询路由
  MARSHALS: "/main/resource/marshals", // 执法队员路由
  LAWCAR: "/main/resource/car", // 执法车辆路由
  DISCRETION: "/main/resource/discretion", // 自由裁量路由
  REGULATION: "/main/system/regulation", // 规章制度路由
  CASE: "/main/business/case", // 案件办理路由
  DUTY: "/main/business/duty", // 勤务管理路由
};

// 控制按钮dom结构-接口权限（根据案件状态、案件类型展示不同按钮） 参数：接口权限、案件状态、案件类型、待办已办
export const permVerifyStatus = (
  permission: string,
  caseStatus: string,
  caseType: string,
  handleId: string,
) => {
  const list = userState.getAccount().permissions;
  const flag = find(list, { permission });
  if (!flag) {
    return false;
  }
  // 公共流程权限判断
  // 未登记（修改、作废、详情）
  if (
    caseStatus === "not_registered" &&
    (permission === PERMISSION_DICT.CASES_UPDATE ||
      permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_DELETE)
  ) {
    return true;
  }
  // 待执行（详情、拒不执行、结案）
  if (
    caseStatus === "pending" &&
    (permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_REFUSE_TO_EXECUTE ||
      permission === PERMISSION_DICT.CASES_CLOSED)
  ) {
    return true;
  }
  // 拒不执行（详情、重新处置、转办、取消立案）
  if (
    caseStatus === "refuse_to_execute" &&
    (permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_REDISPOSE ||
      permission === PERMISSION_DICT.CASES_TRANSFERRED ||
      permission === PERMISSION_DICT.CASES_CANCELLATION)
  ) {
    return true;
  }
  // 不立案、已转办、已结案（详情）
  if (
    (caseStatus === "no_filing" ||
      caseStatus === "transferred" ||
      caseStatus === "closed") &&
    permission === PERMISSION_DICT.CASES_DETAIL
  ) {
    return true;
  }

  // 简易流程权限判断
  // 已登记、简易程序（详情、修改、作废、处置）
  if (
    caseStatus === "registered" &&
    caseType === "simple_procedure" &&
    (permission === PERMISSION_DICT.CASES_UPDATE ||
      permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_DEALWITH ||
      permission === PERMISSION_DICT.CASES_DELETE)
  ) {
    return true;
  }

  // 一般流程权限判断
  // （待执行、拒不执行、立案审核中、已立案、调查中、调查完成、处置审核中）、一般程序（督办）
  if (
    (caseStatus === "pending" ||
      caseStatus === "refuse_to_execute" ||
      caseStatus === "file_reviewing" ||
      caseStatus === "fled" ||
      caseStatus === "survey" ||
      caseStatus === "survey_completed" ||
      caseStatus === "handle_reviewing") &&
    caseType === "general_procedure" &&
    permission === PERMISSION_DICT.CASES_SUPERVISE
  ) {
    return true;
  }
  // （待执行、拒不执行、立案审核中、已立案、调查中、调查完成、处置审核中）、一般程序、已办（催办）
  if (
    (caseStatus === "pending" ||
      caseStatus === "refuse_to_execute" ||
      caseStatus === "file_reviewing" ||
      caseStatus === "fled" ||
      caseStatus === "survey" ||
      caseStatus === "survey_completed" ||
      caseStatus === "handle_reviewing") &&
    caseType === "general_procedure" &&
    handleId &&
    permission === PERMISSION_DICT.CASES_URGE
  ) {
    return true;
  }
  // 立案审核中、一般程序（详情、修改、转办、退回、不立案、立案）
  if (
    caseStatus === "file_reviewing" &&
    caseType === "general_procedure" &&
    (permission === PERMISSION_DICT.CASES_UPDATE ||
      permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_TRANSFERRED ||
      permission === PERMISSION_DICT.CASES_GOBACK ||
      permission === PERMISSION_DICT.CASES_NO_FILLING ||
      permission === PERMISSION_DICT.CASES_FLED)
  ) {
    return true;
  }
  // 已立案、一般程序（详情）
  if (
    caseStatus === "fled" &&
    caseType === "general_procedure" &&
    permission === PERMISSION_DICT.CASES_DETAIL
  ) {
    return true;
  }
  // 调查中、一般程序（详情、上报证据）
  if (
    caseStatus === "survey" &&
    caseType === "general_procedure" &&
    (permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_UPLOADEVIDENCE)
  ) {
    return true;
  }
  // 调查完成、一般程序（详情、重新调查、案件处置）
  if (
    caseStatus === "survey_completed" &&
    caseType === "general_procedure" &&
    (permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_RESURVEY ||
      permission === PERMISSION_DICT.CASES_DEALWITH)
  ) {
    return true;
  }
  // 处置审核中、一般程序（详情、审批同意\重新处置）
  if (
    caseStatus === "handle_reviewing" &&
    caseType === "general_procedure" &&
    (permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_HANDLEAPPROVALAGREE ||
      permission === PERMISSION_DICT.CASES_REDISPOSE)
  ) {
    return true;
  }
  if (
    caseStatus === "not_registered" &&
    (permission === PERMISSION_DICT.CASES_UPDATE ||
      permission === PERMISSION_DICT.CASES_DETAIL ||
      permission === PERMISSION_DICT.CASES_DELETE)
  ) {
    return true;
  }

  return false;
};
